import launchy, string
from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import QVariant

try:
	_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
	_fromUtf8 = lambda s: s

class Ui_PyGTWidget(object):
	def __init__(self):
		self.langs=((1,"Afrikaans","af","0, 0, 1, 1"),(2,"Albanian","sq","1, 0, 1, 1"),(3,"Arabic","ar","2, 0, 1, 1"),(4,"Belarusian","be","3, 0, 1, 1"),(5,"Bulgarian","bg","4, 0, 1, 1"),(6,"Catalan","ca","5, 0, 1, 1"),(7,"Chinese","zh","6, 0, 1, 1"),(8,"Chinese (simpl.)","zh-CN","7, 0, 1, 1"),(9,"Chinese (trad.)","zh-TW","8, 0, 1, 1"),(10,"Croatian","hr","9, 0, 1, 1"),(11,"Czech","cs","10, 0, 1, 1"),(12,"Danish","da","11, 0, 1, 1"),(13,"Dutch","nl","12, 0, 1, 1"),(14,"English","en","0, 1, 1, 1"),(15,"Estonian","et","1, 1, 1, 1"),(16,"Filipino","tl","2, 1, 1, 1"),(17,"Finnish","fi","3, 1, 1, 1"),(18,"French","fr","4, 1, 1, 1"),(19,"Galician","gl","5, 1, 1, 1"),(20,"German","de","6, 1, 1, 1"),(21,"Greek","el","7, 1, 1, 1"),(22,"Haitian (creole)","ht","8, 1, 1, 1"),(23,"Hebrew","iw","9, 1, 1, 1"),(24,"Hindi","hi","10, 1, 1, 1"),(25,"Hungarian","hu","11, 1, 1, 1"),(26,"Icelandic","is","12, 1, 1, 1"),(27,"Indonesian","id","13, 0, 1, 1"),(28,"Irish","ga","13, 1, 1, 1"),(29,"Italian","it","0, 2, 1, 1"),(30,"Japanese","ja","1, 2, 1, 1"),(31,"Korean","ko","2, 2, 1, 1"),(32,"Latvian","lv","3, 2, 1, 1"),(33,"Lithuanian","lt","4, 2, 1, 1"),(34,"Macedonian","mk","5, 2, 1, 1"),(35,"Malay","ms","6, 2, 1, 1"),(36,"Maltese","mt","7, 2, 1, 1"),(37,"Norwegian","no","8, 2, 1, 1"),(38,"Persian","fa","9, 2, 1, 1"),(39,"Polish","pl","10, 2, 1, 1"),(40,"Portuguese","pt","11, 2, 1, 1"),(41,"Portuguese (port.)","pt-PT","12, 2, 1, 1"),(42,"Romanian","ro","13, 2, 1, 1"),(43,"Russian","ru","0, 3, 1, 1"),(44,"Serbian","sr","1, 3, 1, 1"),(45,"Slovak","sk","2, 3, 1, 1"),(46,"Slovenian","sl","3, 3, 1, 1"),(47,"Spanish","es","4, 3, 1, 1"),(48,"Swahili","sw","5, 3, 1, 1"),(49,"Swedish","sv","6, 3, 1, 1"),(50,"Tagalog","tl","7, 3, 1, 1"),(51,"Thai","th","8, 3, 1, 1"),(52,"Turkish","tr","9, 3, 1, 1"),(53,"Ukrainian","uk","10, 3, 1, 1"),(54,"Vietnamese","vi","11, 3, 1, 1"),(55,"Welsh","cy","12, 3, 1, 1"),(56,"Yiddish","yi","13, 3, 1, 1"))

	def setupUi(self, PyGTWidget):
		PyGTWidget.setObjectName(_fromUtf8("PyGTWidget"))
		PyGTWidget.resize(430, 360)
		self.gridLayoutWidget = QtGui.QWidget(PyGTWidget)
		self.gridLayoutWidget.setGeometry(QtCore.QRect(10, 10, 421, 351))
		self.gridLayoutWidget.setObjectName(_fromUtf8("gridLayoutWidget"))
		self.gridLayout = QtGui.QGridLayout(self.gridLayoutWidget)
		self.gridLayout.setObjectName(_fromUtf8("gridLayout"))
		self.checkBox=dict()
		for lang in self.langs:
			self.checkBox[lang[0]]=QtGui.QCheckBox(self.gridLayoutWidget)
			self.checkBox[lang[0]].setObjectName(_fromUtf8("checkBox_" + lang[2]))
			(a,b,c,d)=[int(i) for i in lang[3].split(', ')]
			self.gridLayout.addWidget(self.checkBox[lang[0]],a,b,c,d)
			#self.checkBox[1].setChecked(True)

		self.retranslateUi(PyGTWidget)
		QtCore.QMetaObject.connectSlotsByName(PyGTWidget)

	def retranslateUi(self, PyGTWidget):
		PyGTWidget.setWindowTitle(QtGui.QApplication.translate("PyGTWidget", "PyGT settings", None, QtGui.QApplication.UnicodeUTF8))
		for lang in self.langs:
			self.checkBox[lang[0]].setText(QtGui.QApplication.translate("PyGTWidget",lang[1],None, QtGui.QApplication.UnicodeUTF8))
			
	def Checked(self):
		checked_langs=list()
		for lang in self.langs:
			if (self.checkBox[lang[0]].isChecked()):
				checked_langs.append(lang[2])
			else:
				checked_langs.append("")
		return "|" + "|".join(checked_langs) + "|"
		
	def Langs(self):
		return self.langs
		
class PyGTUi(QtGui.QWidget):
	def __init__(self, parent=None):
		QtGui.QWidget.__init__(self, parent)
		self.ui = Ui_PyGTWidget()
		self.ui.setupUi(self)
		
		settings = launchy.settings
		a=settings.value("PyGT/langs/value").toString()
		langs=a[1:-1].split("|")
		for lang in self.ui.Langs():
			if (string.find(str(a),"|"+ lang[2] +"|") != -1): self.ui.checkBox[lang[0]].setChecked(True)
			
	def writeSettings(self):
		settings = launchy.settings

		# Add all rows to the dirs array
		settings.remove("PyGT/langs")
		settings.remove("PyGT/langs/value")
		settings.setValue("PyGT/langs/value", QVariant(self.ui.Checked()))
